programming4us
           
 
 
Programming

.NET Components : Serialization and Class Hierarchies (part 1) - Custom Serialization and Base Classes

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
6/30/2013 9:23:50 PM

When you apply the Serializable attribute to a class, it affects only that class—it doesn't make any derived classes serializable, because the Inherited property of the AttributeUsage attribute applied on the Serializable attribute is set to false. For example, if you derive MyClass from MyBaseClass, MyClass isn't serializable:

    [Serializable]
    public class MyBaseClass
    {}
    public class MyClass : MyBaseClass
    {}

At first glance this may appear awkward, but it does make design sense: MyBaseClass has no way of knowing whether its subclasses will have non-serializable members, so it would be wrong for them to automatically inherit the serializable status. If you design a class hierarchy and you want to support serialization of any type in the hierarchy, be sure to mark each level with the Serializable attribute:

    [Serializable]
    public class MyBaseClass

    {}
    [Serializable]
    public class MyClass : MyBaseClass
    {}
    [Serializable]
    public class MyOtherClass : MyClass
    {}

1. Custom Serialization and Base Classes

If any of the classes in the hierarchy implements ISerializable, there are a few design guidelines you have to follow to allow subclasses to provide their own custom serialization and to correctly manage the custom serialization of the base classes:

  • Only the topmost base class that uses custom serialization needs to derive from ISerializable.

  • When using implicit interface implementation, a base class must define its GetObjectData( ) method as virtual to allow subclasses to override it. 

  • In a subclass implementation of GetObjectData( ), after serializing its own state, the subclass must call its base class's implementation of GetObjectData( ).

  • In its implementation of the deserializing constructor, the subclass must call its base class's deserializing constructor.

  • The deserializing constructor should be protected, to allow a subclass to call its base class's deserializing constructor.

Example 1 demonstrates how to implement these points; you can use it as a template for combining custom serialization and class hierarchies.

Example 1. Combining custom serialization with a class hierarchy
[Serializable]
public class MyBaseClass : ISerializable
{
   public MyBaseClass(  )
   {}
   int m_BaseNumber;
   public virtual void GetObjectData(SerializationInfo info,
                                     StreamingContext context)
   {
      //Add MyBaseClass members
      GenericSerializationInfo genericInfo = new GenericSerializationInfo(info);
      genericInfo.AddValue("m_BaseNumber",m_BaseNumber);
   }
   protected MyBaseClass(SerializationInfo info,StreamingContext context)
   {
      //Read MyBaseClass members and initialize them
      GenericSerializationInfo genericInfo = new GenericSerializationInfo(info);

      m_BaseNumber = genericInfo.GetValue<int>("m_BaseNumber");
   }
}

[Serializable]
public class MySubClass : MyBaseClass
{
   public MySubClass(  )
   {}
   int m_SubNumber;
   public override void GetObjectData(SerializationInfo info,
                                      StreamingContext context)
   {
      //Add MySubClass members
      GenericSerializationInfo genericInfo = new GenericSerializationInfo(info);
      genericInfo.AddValue("m_SubNumber",m_SubNumber);
      base. GetObjectData(info,context);
   }
   protected MySubClass(SerializationInfo info,StreamingContext context):

                                                                base(info,context)
   {
      //Read MySubClass members and initialize them
      GenericSerializationInfo genericInfo = new GenericSerializationInfo(info);
      m_SubNumber = genericInfo.GetValue<int>("m_SubNumber");
   }
}

					  

If a base class provides custom serialization, all subclasses derived from it can use only custom serialization.

Other -----------------
- .NET Components : Custom Serialization (part 2) - Constraining Serialization
- .NET Components : Custom Serialization (part 1) - The ISerializable Interface, Implementing ISerializable
- .NET Components : Serialization and Streams - Serializing Multiple Objects
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 5) - Advanced HTTP Handler Programming
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 4) - Serving Images More Effectively
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 3) - The Picture Viewer Handler
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 2) - An HTTP Handler for Quick Data Reports
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 1) - The IHttpHandler Interface
- Microsoft ASP.NET 3.5 : HTTP Handlers and Modules - Quick Overview of the IIS Extensibility API
- Programming WCF Services : Queued Services - The HTTP Bridge
- Microsoft ASP.NET 4 : Ajax - Extender Controls (part 2) - A Modal Pop-up Dialog-Style Component
- Microsoft ASP.NET 4 : Ajax - Extender Controls (part 1) - The AutoComplete Extender
- Mobile Handheld Devices : DATA SYNCHRONIZATION
- Mobile Handheld Devices : MEMORY, STORAGE AND BATTERIES
- LINQ to Objects : How to Return Elements When the Result Is a Sequence (Select Many)
- LINQ to Objects : How to Change the Return Type (Select Projection)
- A Technical Overview of the Mobile Web : OTHER MOBILE TECHNOLOGIES
- A Technical Overview of the Mobile Web : THE MOBILE NETWORK
- Programming WCF Services : The Response Service (part 4) - Transactions
- Programming WCF Services : The Response Service (part 3) - Queued Service-Side Programming & Response Service-Side Programming
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us